<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use User\Api\UserApi as UserApi;

class RejectController extends AdminController {
    public function rejectlist() {
        if(I('b_date')){
            $map['createdAt'] = array('gt',strtotime(I('b_date')));
        }
        if(I('e_date')){
            $map['createdAt'] = array('lt',strtotime(I('e_date')));
        }
        if(I('b_date')&&I('e_date')){
            $map['createdAt'] = array('between',array(strtotime(I('b_date')),strtotime(I('e_date'))));
        }
        if(I('pname')){
            $map['pname'] = array('like',I('pname'));
        }
        if(I('buyer')){
            $map['buyer'] = array('like',I('buyer'));
        }
        $map['status'] = array('neq',-1);
        $list = $this->lists('reject',$map,'createdAt desc');
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '数据列表';
        $this->display();
    }
    public function newreject(){
        if(IS_POST){
            $buyer = I('buyer');
            $id_array = I('id');
            $cnt_array = I('cnt');
            $reason_array = I('reason');
            for ($i = 0; $i < count($id_array); $i++) {
                if ($id_array[$i] == "" || $cnt_array[$i] == ""||$buyer=="") {
                    $this->error("退货信息不能为空");
                }
            }
            for ($i = 0; $i < count($id_array); $i++) {
                $Inventory = M("Inventory"); // 实例化Inventory对象
                $map2['id'] = $id_array[$i];
                $info = $Inventory->where($map2)->find();
                $dataList[] = array(
                    'pid' => $info['pid'],
                    'pname' => $info['pname'],
                    'spec' => $info['spec'],
                    'num' => $cnt_array[$i],
                    'createdAt' => NOW_TIME,
                    'updatedAt' => NOW_TIME,
                    'user' => UID,
                    'buyer' => $buyer,
                    'reason' => $reason_array[$i],
                    'status' => 1000
                );
            }
            if(D('reject')->addAll($dataList)){
                $this->success("新增成功",U('rejectlist'));
            }else{
                $this->error("新增失败");
            }
        }else{
            $map['status'] = 1;
            $prolist = $this->lists('Inventory', $map,'createdAt asc');
            $this->assign( 'product', json_encode($prolist) );
            $this->display();
        }
    }
    public function dealreject(){
        if(IS_POST){
            $id = I("id");
            $spec_arr = I("spec");
            $num_arr = I("num");
            $pname_arr = I("pname");
            $buyer_arr = I("buyer");
            $action_arr = I("action");
            $data2['status'] = 1000;
            if(M('reject')->where(array("id"=>$id))->getField("status") > 1000){
                $this->error("该订单已被处理！");
            }
            for($i=0;$i<count($action_arr);$i++){
                if($num_arr[$i]==""||$num_arr[$i]==0){
                    continue;
                }
                switch($action_arr[$i]){
                    case "zzz":
                        $this->dealRejectDetail($id,$pname_arr[$i],$spec_arr[$i],$num_arr[$i],"zzz",$buyer_arr[$i]);
                        break;
                    case "rk":
                        $map['spec'] = $spec_arr[$i];
                        $map['pname'] = $pname_arr[$i];
                        $this->dealRejectDetail($id,$pname_arr[$i],$spec_arr[$i],$num_arr[$i],"rk",$buyer_arr[$i]);
                        M('inventory')->where($map)->setInc('cnt',$num_arr[$i]);
                        break;
                    case "bs":
                        $this->dealRejectDetail($id,$pname_arr[$i],$spec_arr[$i],$num_arr[$i],"bs",$buyer_arr[$i]);
                        break;
                }
                $data2['status'] = 2000;
            }
            if($data2['status'] == 2000){
                M("reject")->where(array("id"=>$id))->save($data2);
                $this->success("退单处理成功",U('rejectlist'));
            }else{
                $this->error("没有数据变化");
            }
        }else{
            $id=I('id');
            $map['id'] = $id;
            $list = M('reject')->where($map)->select();
            $map2['pname'] = $list[0]['pname'];
            $prolist = M('Inventory')->where($map2)->select();
            $this->assign( 'product', json_encode($prolist) );
            $this->assign( 'pname', $list[0]['pname'] );
            $this->assign( 'buyer', $list[0]['buyer'] );
            $this->assign('_list', $list);
            $this->display();
        }

    }
    public function dealRejectDetail($rejectid,$pname,$spec,$num,$action,$buyer){
        $data['rejectid'] = $rejectid;
        $data['pname'] = $pname;
        $data['spec'] = $spec;
        $data['num'] = $num;
        $data['user'] = UID;
        $data['action'] = $action;
        $data['buyer'] = $buyer;
        $data['createdAt'] = time();
        M('reject_goods')->add($data);
    }
    public function detail(){
        $map2['id'] = I('rejectid');
        $list = M('reject')->where($map2)->select();
        $map['rejectid'] = I('rejectid');
        $info = M('reject_goods')->where($map)->select();
        $this->assign("info",$info);
        $this->assign("_list",$list);
        $this->display();
    }
    public function data(){
        $date = I('date');
        $action = I('action');
        $pname = I('pname');
        $actions = array('1'=>array('action'=>'zzz','content'=>'转榨汁'),'2'=>array('action'=>'rk','content'=>'入库'),'3'=>array('action'=>'bs','content'=>'报损'));
        $map['action'] = "zzz";
        if($date){
            $map['createdAt'] = array('between',array(strtotime($date),strtotime($date)+86400));
        }
        if($action){
            $map['action'] = $action;
        }
        if($pname){
            $map['pname'] = $pname;
        }
        $map['status'] = 1;
        $info = $this->lists('reject_goods',$map);
        $this->assign("info",$info);
        $this->assign("actions",$actions);
        $this->action = $action;;
        $this->display();
    }

}
